package com.heart.house.web.controller;

import com.heart.house.biz.service.AgencyService;
import com.heart.house.biz.service.HouseService;
import com.heart.house.biz.service.RecommandService;
import com.heart.house.common.constants.CommonConstants;
import com.heart.house.common.model.House;
import com.heart.house.common.model.User;
import com.heart.house.common.page.PageData;
import com.heart.house.common.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Luo Zhen
 * @create 2019-06-26 16:12
 */
@Controller
public class AgencyController {

    @Autowired
    private AgencyService agencyService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private RecommandService recommandService;

    /**
     * 经纪人列表
     *
     * @param pageSize
     * @param pageNum
     * @param modelMap
     * @return
     */
    @RequestMapping("/agency/agentList")
    public String agentList(Integer pageSize, Integer pageNum, ModelMap modelMap) {
        PageData<User> ps = agencyService.getAllAgent(PageParams.build(pageSize, pageNum));
        List<House> hotHouses = recommandService.getHostList(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses", hotHouses);
        modelMap.put("ps", ps);
        return "/user/agent/agentList";
    }

    /**
     * 经纪人详情列表
     *
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("/agency/agentDetail")
    public String agentDetail(Long id, ModelMap modelMap) {
        User user = agencyService.getAgentDetail(id);
        // 防止模拟用户详情请求跳转到主页
        if (user == null) {
            return "redirect:agentList";
        }
        House house = new House();
        house.setUserId(id);
        house.setBookmarked(false);
        PageData<House> pageData = houseService.queryHouse(house, new PageParams(3, 1));
        if (pageData != null) {
            modelMap.put("bindHouses", pageData.getList());
        }
        List<House> hotHouses = recommandService.getHostList(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses", hotHouses);
        modelMap.put("agent", user);
        return "/user/agent/agentDetail";
    }
}
